package com.sunyur.tailor.channel.service.inner.contract.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sunyur.tailor.channel.common.inner.base.SunyurListResult;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.inner.util.SunyurResultUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelListResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelListResultUtil;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.contract.model.ContractInfoModel;
import com.sunyur.tailor.channel.service.inner.contract.model.ContractPaymentStageModel;
import com.sunyur.tailor.channel.service.inner.contract.param.ContractInfoParam;
import com.sunyur.tailor.channel.service.inner.contract.param.CreateContractParam;
import com.sunyur.tailor.channel.service.inner.contract.param.GetContractInfoParam;
import com.sunyur.tailor.channel.service.inner.contract.service.ContractService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: HuBaoding
 * @Description:
 * @Date: Created in 11:03 2021/9/7
 */
@Slf4j
@Service("contractService")
public class ContractServiceImpl implements ContractService {


    /**
     * 请求商越Client
     */
    @Autowired
    private SunyurClientUtil sunyurClient;

    @Value("${sunyur.api.createBatchContract}")
    private String createBatchContract;

    @Value("${sunyur.api.listContractStageByCodes}")
    private String listContractStageByCodesUrl;

    @Value("${sunyur.api.listContract}")
    private String listContract;


    /**
     * 合同台账批量同步接口
     *
     * @param param
     * @return
     */
    @Override
    public ChannelResult<Boolean> createBatchContract(List<CreateContractParam> param) {
        log.info("ContractServiceImpl createBatchContract param is {} ", JSON.toJSONString(param));
        SunyurResult<Boolean> sunyurResult = null;
        try {
            sunyurResult = sunyurClient.execPost(createBatchContract, JSON.toJSONString(param), new TypeReference<SunyurResult<Boolean>>() {
            });
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
        } catch (Exception e) {
            log.error("ContractServiceImpl createBatchContract fail,param is {} ", JSON.toJSONString(param));
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), e.getMessage());
        }
        log.info("ContractServiceImpl createBatchContract param is {} , result is {}", JSON.toJSONString(param), JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    /**
     * 根据合同编码查询合同付款条款
     * 查询不会超过100个，暂时不写循环
     */
    @Override
    public ChannelListResult<ContractPaymentStageModel> listPaymentStageByContractCodes(GetContractInfoParam param) {
        SunyurListResult<ContractPaymentStageModel> sunyurListResult = null;
        try {
            sunyurListResult = sunyurClient.execListPost(listContractStageByCodesUrl, JSON.toJSONString(param), new TypeReference<SunyurListResult<ContractPaymentStageModel>>() {
            });
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurListResult)) {
                return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurListResult.getErrorMessage());
            }
        } catch (Exception e) {
            log.error("ContractServiceImpl listPaymentStageByContractCodes fail,param is {} ", JSON.toJSONString(param));
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), e.getMessage());
        }
        log.info("ContractServiceImpl listPaymentStageByContractCodes param is {} , result is {}", JSON.toJSONString(param), JSON.toJSONString(sunyurListResult));
        return ChannelListResultUtil.success(sunyurListResult.getContent());
    }

    @Override
    public ChannelListResult<ContractInfoModel> listContract(ContractInfoParam param) {
        log.info("ContractServiceImpl listContract param is {}", JSON.toJSONString(param));
        if (ObjectUtils.isEmpty(param)) {
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), "ContractServiceImpl listContract param is null");
        }
        // 调用主线接口
        SunyurListResult<ContractInfoModel> sunyurResult = sunyurClient.execListPost(listContract, JSON.toJSONString(param), new TypeReference<SunyurListResult<ContractInfoModel>>() {
        });
        // 主线报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelListResultUtil.success(sunyurResult.getContent());
    }
}
